import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo_k/view/custom_app_bar.dart';

import '../change_notifier/count_change_notifier.dart';
import '../change_notifier/notifier_provider.dart';
import '../widget/refresh_widget.dart';

class InheritedNotifierPage extends StatefulWidget {
  static const String routeName = '/inherited_notifier_page';
  const InheritedNotifierPage({super.key});

  @override
  State<InheritedNotifierPage> createState() => _InheritedNotifierPageState();
}

class _InheritedNotifierPageState extends State<InheritedNotifierPage> {
  late CounterChangeNotifier counter;
  late String data;
  late GlobalKey key = GlobalKey();
  @override
  void initState() {
    super.initState();
    Future(() {
      // 在 didChangeDependencies 中获取 InheritedWidget 实例
      final arguments = ModalRoute.of(context)!.settings.arguments as Map;
      data = arguments['data'] as String;
      print(data);
      print('000000000000000000000000000000');
    });
  }

  //这个方法在小部件第一次被插入到小部件树中时调用，或者当它依赖的任何 InheritedWidget 发生变化时被再次调用
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    counter = NotifierProvider.of(context);
    print('我改变了');
  }

  @override
  Widget build(BuildContext context) {
    print('InheritedNotifierPage..re..builder....${InheritedNotifierPage}');
    return Scaffold(
      appBar: CustomAppBar(
        title: 'ChangeNotifier + InheritedWidget 示例',
      ),
      body: Container(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                '你已经点击了按钮这么多次:',
              ),
              Text(
                '${counter.count}', // 显示计数
                style: Theme.of(context).textTheme.headline4,
              ),
              RepaintBoundary(
                key: key,
                child: const RefreshWidget(
                  name: 'krcm110',
                ),
              ),
              const RefreshWidget(
                name: 'krcm111',
              )
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: counter.increment, // 点击时调用 increment
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
